using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interactor_SeaGrassDoor_Ctrl : Unit_Interactor_Ctrl
{
    public Animator animator;

    public new Collider2D collider;

    public bool isOpen;

    [SerializeField]
    private SpriteRenderer lightBulb_Light;
    [SerializeField]
    private List<Transform> rootPoints;
    private Transform rootPoint
    {
        get
        {
            if (rootPoints.Count > 0)
            {
                return rootPoints[rootPoints.Count - 1];
            }
            return null;
        }
    }
    [SerializeField]
    private Transform topPoint;
    [SerializeField]
    private Transform bottomPoint;

    private Transform closeRootPoint
    {
        get
        {
            if (rootPoint != null)
            {
                if ((rootPoint.transform.position - topPoint.transform.position).magnitude < (rootPoint.transform.position - bottomPoint.transform.position).magnitude)
                {
                    return topPoint;
                }
                else
                {
                    return bottomPoint;
                }
            }
            return null;
        }
    }

    [SerializeField]
    private LineRenderer root_Line;

    public float durationTime = 2;

    public IEnumerator switchDoorIE;


    private void OnDrawGizmosSelected()
    {
        DrawRootLine();
    }
    public void DrawRootLine()
    {
        if (closeRootPoint != null )
        {
            List<Transform> rootPoints = new List<Transform>();
            rootPoints.AddRange(this.rootPoints.ToArray());
            rootPoints.Add(closeRootPoint);
            List<Vector3> poss = new List<Vector3>();
            for (int i = 0; i < rootPoints.Count - 2; i++)
            {
                Vector3 direction = rootPoints[i + 1].position - rootPoints[i + 2].position;
                direction = direction.normalized * (rootPoints[i].position - rootPoints[i + 1].position).magnitude * 0.5f;
                Bezier bezier = new Bezier(rootPoints[i].position, rootPoints[i + 1].position + direction, rootPoints[i + 1].position);
                for (int k = 0; k < 5; k++)
                {
                    poss.Add(bezier.Lerp(k / (5f - 1)));
                }
            }
            poss.Add(closeRootPoint.position);


            if (root_Line != null)
            {
                root_Line.positionCount = poss.Count;
                root_Line.SetPositions(poss.ToArray());
            }
        }
    }
    private void Start()
    {
        DrawRootLine();
        SwitchDoor(isOpen);
    }

    public override void ReceiveLight(Vector3 sourcePos, Vector3 sourceDirection)
    {
        
        base.receiveLightEvent?.Invoke(sourcePos, sourceDirection);
        if (isOpen)
        {
            return;
        }
        SwitchDoorPerformance(true,()=> {
            IEPool_Manager.instance.WaitTimeToDo(ref switchDoorIE, durationTime, null, () => {
                SwitchDoorPerformance(false);
            });
        });
    }

    public void SwitchDoor(bool isOpen)
    {
        IEPool_Manager.instance.StopIE(switchDoorIE);
        this.isOpen = isOpen;
        animator.SetFloat("Blend", isOpen ? 1 : 0);
        lightBulb_Light.color = new Color(lightBulb_Light.color.r, lightBulb_Light.color.g, lightBulb_Light.color.b, isOpen ? 1 : 0);

        collider.transform.localScale = isOpen ? Vector3.zero : Vector3.one;
        //collider.gameObject.SetActive(!isOpen);
    }
    public void SwitchDoorPerformance(bool isOpen, System.Action finishAction = null)
    {
        this.isOpen = isOpen;
        AnimationCurve target_Curve = isOpen ? GameManager.instance.curve_Group["slowToFast"] : GameManager.instance.curve_Group["fastToSlow"];
        float initBlend = animator.GetFloat("Blend");
        float targetBlend = isOpen ? 1 : 0;
        float initLightBulb_LightAlpha = lightBulb_Light.color.a;
        float targetLightBulb_LightAlpha = isOpen ? 1 : 0;
        float time = 1;
        Vector3 initColliderSize = new Vector3(0, 1, 1);
        Vector3 targetColliderSize = isOpen ? new Vector3(0,1,1) : Vector3.one;
        IEPool_Manager.instance.KeepTimeToDo(ref switchDoorIE, time, null, (_time) =>
        {
            animator.SetFloat("Blend", Mathf.Lerp(initBlend, targetBlend, target_Curve.Evaluate( 1 - _time / time)));
            lightBulb_Light.color = new Color(lightBulb_Light.color.r, lightBulb_Light.color.g, lightBulb_Light.color.b, Mathf.Lerp(initLightBulb_LightAlpha, targetLightBulb_LightAlpha, 1 - (_time- time * 0.5f) / (time - time * 0.5f)));
            collider.transform.localScale = Vector3.Lerp(initColliderSize, targetColliderSize, 1 - _time / time);
            return true;
        }, () =>
        {
            animator.SetFloat("Blend", targetBlend);
            lightBulb_Light.color = new Color(lightBulb_Light.color.r, lightBulb_Light.color.g, lightBulb_Light.color.b, targetLightBulb_LightAlpha);
            collider.transform.localScale = targetColliderSize;
            finishAction?.Invoke();
        });
    }
}
